package cn.lds.healthexamspringboot.controller;

import cn.lds.healthexamspringboot.domain.HealthExamItem;
import cn.lds.healthexamspringboot.domain.HealthExamPackage;
import cn.lds.healthexamspringboot.domain.User;
import cn.lds.healthexamspringboot.dto.ResponseDTO;
import cn.lds.healthexamspringboot.service.HealthExamItemService;
import cn.lds.healthexamspringboot.service.HealthExamPackageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;


@Controller
@RequestMapping("exam_pack")
public class HealthExamPackageController {

    @Resource
    private HealthExamPackageService healthExamPackageService;

    @Resource
    private HealthExamItemService healthExamItemService;

    private Logger log = LoggerFactory.getLogger(HealthExamPackageController.class);

    @GetMapping("add_exam_package")
    public String add_pack(Model model){

        ResponseDTO<List<HealthExamItem>> listResponseDTO = healthExamItemService.selectItemAll();

        model.addAttribute("item",listResponseDTO.getData());

        return "add_exam_package";
    }

    @GetMapping("do_add_package")
    public String doAddPackage(HealthExamPackage examPackage, Integer[] itemIds, HttpSession session) throws Exception {

        log.info("items{}", Arrays.toString(itemIds));
        User loginUser = (User) session.getAttribute("login");

        examPackage.setCreateBy(Long.valueOf(loginUser.getId()));


        ResponseDTO responseDTO = healthExamPackageService.addExamPackage(examPackage, itemIds);
        return "redirect:/";
    }

}
